﻿using libedi.Model.edi;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeBackup.Code
{
    public class UpBackDB
    {
        Upload upload = new Upload();
        private string FilePath;

        public UpBackDB(string FilePath)
        {
            this.FilePath = FilePath;

        }


        public void UpBack()
        {
            switch (Path.GetExtension(FilePath))
            {
                case ".MZLB":
                    {
                        MZLB(FilePath);
                        break;
                    }
                case ".zip":
                    {
                        zip(FilePath);
                        break;
                    }
                case ".tdb":
                    {
                        tdb(FilePath);
                        break;
                    }
                default:
                    {
                        throw new NotImplementedException();
                    }
            }
        }

        private void MZLB(string FilePath)
        {
            zlb zlb = new zlb();
            string file = zlb.ZlbToZip(FilePath);
            if (string.IsNullOrEmpty(file))
            {
                return;
            }
            updatas(file, "codebackup_reback_zip");
        }


        private void zip(string FilePath)
        {

            updatas(FilePath, "codebackup_reback_zip");
        }


        private async void tdb(string FilePath)
        {
            updatas(FilePath, "codebackup_reback_tdb");
        }

        async void updatas(string FilePath,string methodID)
        {
            string file = await upload.upAsync(FilePath);

            if (string.IsNullOrEmpty(file))
            {
                throw new NullReferenceException();
            }
            Dictionary<string, string> valuePairs = JsonConvert.DeserializeObject<Dictionary<string, string>>(file);
            CodeConfig.mqtt.Send(methodID, valuePairs["fileName"]);
        }
    }

  
}
